//
//  ZXX EmoticonPictureViewCell.swift
//  M-Weibo
//
//  Created by ZXX on 16/4/25.
//  Copyright © 2016年 ZXX. All rights reserved.
//

import UIKit
///表情按钮距离边框的距离
let EmoticonButtonMargin:CGFloat = 5
///表情下面pageControl的高度
let EmoticonPageControllerHeight: CGFloat = 20


class ZXXEmoticonPictureViewCell: UICollectionViewCell {
    //保存全部按钮数组
    var buttons:[ZXXEmoticonButton] = [ZXXEmoticonButton]()
    
    // 表情一维数组
    var emoticons: [ZXXEmoticon]?{
        didSet{
            
            
            // 遍历我们的buttons(隐藏所有button)
            for button in buttons{
                button.hidden = true
            }
            
            
            for (i, emoticon) in emoticons!.enumerate(){
                
                let button = buttons[i]
                // 给我们的自定义button的emoticon的属性赋值
                button.emoticon = emoticon
                // 显示button
                button.hidden = false
                // 代表emoji表情
                if emoticon.isEmoji{
                    // 设置title
                    button.setTitle(((emoticon.code ?? "") as NSString).emoji(), forState: UIControlState.Normal)
                    // 解决cell重用问题
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                }else{
                    // 代表图片表情
                    
                    let path = "\(emoticon.path ?? "")\(emoticon.png ?? "")"
                    let image = UIImage(named: path, inBundle: ZXXEmoticonTools.sharedZXXEmoticonTools.emoticonBundle, compatibleWithTraitCollection: nil)
                    
                    button.setImage(image, forState: UIControlState.Normal)
                    // 解决cell重用问题
                    button.setTitle(nil, forState: UIControlState.Normal)
                    
                }
                
            }
            
        }
    }

    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置视图
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  MARK: -  设置视图
    private func setupUI(){
        self.backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addChildButtons()
        contentView.addSubview(deleteButton)

        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        // button 的宽 高
        let w = (SCREENWIDTH - EmoticonButtonMargin * 2) / CGFloat(ZXXEmoticonMaxCol)
        let h = (EmoticonKeyboardHeight - EmoticonKeyboardToolBarHeight - EmoticonPageControllerHeight) / CGFloat(ZXXEmoticonMaxRow)
        
        // 设置frame
        // 遍历数组
        for (i, button) in buttons.enumerate(){
            let x = i % ZXXEmoticonMaxCol
            let y = i / ZXXEmoticonMaxCol
            button.frame = CGRect(x: CGFloat(x) * w + 5, y: CGFloat(y) * h, width: w, height: h)
        }
        
        // 设置删除按钮的frame
        deleteButton.frame = CGRect(x: SCREENWIDTH - 5 - w, y: h * 2, width: w, height: h)
        
    }

    //  MARK: -  删除控件
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        //添加删除按钮的点击事件
        button.addTarget(self, action: #selector(ZXXEmoticonPictureViewCell.deleteButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    // 添加表情按钮
    func addChildButtons(){
        // 创建button
        for _ in 0..<ZXXEmoticonMaxCount{
            let button = ZXXEmoticonButton()
            // 添加事件
            button.addTarget(self, action: #selector(ZXXEmoticonPictureViewCell.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            contentView.addSubview(button)
            buttons.append(button)
        }
    }
    
    
    //  MARK: -  添加表情的监听
    // MARK: - 表情按钮点击
    func buttonClick(button: ZXXEmoticonButton){
        
        // 发送通知 告知发布微博控制器
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonButtonNSNotification, object: button.emoticon)
    }
    func deleteButtonClick(){
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonDeleteButtonNSNotification, object: nil)
    }

}
